package Element;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import Core.GLives;
import Core.RGBFont;
import Core.Slide;

public class Dialog {
	final short HDESCTEXT = 18;
	
	public int iFrameCnt = 0;
	private int x, y, w, h;
	private String text; private String arrT[];
	private Slide sl = GLives.ins.currSlide;
	private Image imgDialog = null;
	public void show(String content){
		text=content;
		iFrameCnt = 50;
		rezise();
		}
	public void show_static(String content){
		show(content);
		iFrameCnt=-1;
		}
	public void hide(){
		iFrameCnt = 0;
		}
	public void update(){
		if(iFrameCnt > 0)
			iFrameCnt--;
		}
	public void drawMe(Graphics g){
		if(iFrameCnt==0 || arrT == null) return;
		g.setClip(sl.inX, sl.inY, sl.inW, sl.inH);
		paintBFrame(g, x, y, w, h, imgDialog, 18, 16, 11, 9, 20, 70, 16, 20, true);
		for(int i = 0; i < arrT.length; i++)
			RGBFont.fnormal.drawString(g, arrT[i], GLives.ScreenWidth>>1, y + 13 + HDESCTEXT*i, 2, 0x003300);
		}
	public void rezise(){
		if(iFrameCnt==0 || text == null || text.length() == 0) return;
		w = sl.inW - 50;
		arrT = RGBFont.fnormal.splitStrInLine(text, w - 10);
		h = arrT.length*18 + 20;
		x = (GLives.ScreenWidth - w)>>1;
		y = GLives.ScreenHeight - sl.inY - h - 70;
		}
	private void paintBFrame(Graphics g, int x, int y, int w, int h, Image imgborder, int connerW, int connerH, int leftW, int topH, int xH, int wH, int yV, int hV, boolean hasFill){
		int rw = (connerW - leftW); 
		int rh = (connerH - topH );
		g.drawRegion(imgborder, 0, 0, connerW, connerH, 0, x - leftW, y - topH, 0);
		g.drawRegion(imgborder, 0, imgborder.getHeight() - connerH, connerW, connerH, 0,x - leftW, y + h - rh + 1, 0);
		int kc = (w - 2*rw + 1) % wH;
		int temp = x + w - rw;
		for(int i = x + rw; i < temp; i +=kc, kc = wH){
			g.drawRegion(imgborder, xH, 0, kc, topH, Sprite.TRANS_NONE, i, y, Graphics.BOTTOM|Graphics.LEFT);
			g.drawRegion(imgborder, xH, 0, kc, topH, Sprite.TRANS_ROT180, i, y + h + 1, 0);
		}
		kc = (h - 2*rh + 1) % hV;
		temp = y + h - rh;
		for(int i = y + rh; i < temp; i+=kc, kc=hV){
			g.drawRegion(imgborder, 0, yV, leftW, kc, Sprite.TRANS_NONE, x, i, Graphics.RIGHT|Graphics.TOP);
			g.drawRegion(imgborder, 0, yV, leftW, kc, Sprite.TRANS_ROT180, x + w + 1, i, 0);
		}
		g.drawRegion(imgborder, imgborder.getWidth() - connerW, 0, connerW, connerH, 0, x + w - rw + 1, y - topH, 0);
		g.drawRegion(imgborder, imgborder.getWidth() - connerW, imgborder.getHeight() - connerH, connerW, connerH, 0, x + w - rw + 1, y + h - rh + 1, 0);
		if(hasFill){
			/** // Fill with inner Image 
//			int wB = imgborder.getWidth() - 2*connerW + 1;
//			int hB = imgborder.getHeight() - 2*connerH + 1;
//			for(int i = x; i < x+w; i +=wB){
//				for(int j = y; j < y+h; j+=hB){
//					g.drawRegion(imgborder, connerW, connerH, wB, hB, 0, i, j, 0);
//				}
//			} */
			g.setColor(0xEFF7EE);
			g.fillRect(x + rw, y, w - 2*rw + 1, rh);
			g.fillRect(x + rw, y + h - rh + 1, w - 2*rw + 1, rh);
			g.fillRect(x, y + rh, w + 1, h - 2*rh + 1);
		}
		}
}
